﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports BusinessRule.IBusiness
Imports DatabaseAccess.IDAL
Imports System.Data
Imports BusinessRule.Common
Imports DatabaseAccess.DALFactory
Imports External.APICalling
Imports BusinessRule.DTO
Imports System.Xml
Imports System.Configuration

Public Class uMESDocViewBusiness
    Implements uMESDocViewIBusiness
    Private dal As IOracleDAL = uMESDalContext.instance.GetDalObject()

    ''' <summary>
    ''' 读取产品树的根节点
    ''' </summary>
    ''' <returns></returns>
    Private Function LoadProductRootInfo(ByVal QueryCondition As Dictionary(Of String, String)) As uMESProcessResult Implements uMESDocViewIBusiness.LoadProductRootInfo
        Dim oResult As New uMESProcessResult

        Try
            Dim strSQL, strProductName, strProductRev, strProductDesc As String
            strProductName = ""
            If QueryCondition.Keys.Contains("ProductName") = True Then
                strProductName = QueryCondition("ProductName").ToString()
            End If
            strProductRev = ""
            If QueryCondition.Keys.Contains("ProductRev") = True Then
                strProductRev = QueryCondition("ProductRev").ToString()
            End If
            strProductDesc = ""
            If QueryCondition.Keys.Contains("ProductDesc") = True Then
                strProductDesc = QueryCondition("ProductDesc").ToString()
            End If
            strSQL = "SELECT Product.Productid" & vbCrLf & _
                   "FROM Product " & vbCrLf & _
                   "INNER JOIN ProductBase ON Product.Productbaseid = ProductBase.Productbaseid WHERE 1=1" & vbCrLf
            If strProductName <> "" Then
                strSQL = strSQL & "AND ProductBase.Productname = '" & strProductName & "'" & vbCrLf
            End If

            If strProductRev <> "" Then
                strSQL = strSQL & "AND Product.Productrevision = '" & strProductRev & "'" & vbCrLf
            Else
                strSQL = strSQL & "AND Product.Productid = ProductBase.Revofrcdid " & vbCrLf
            End If

            If strProductDesc <> "" Then
                strSQL = strSQL & "AND Product.Description LIKE '%" & strProductDesc & "%'" & vbCrLf
            End If
            strSQL = strSQL & "ORDER BY Product.Productid" & vbCrLf

            Dim paraPage As New uMESPagingDataDTO
            paraPage.CurrentPageIndex = Convert.ToInt32(QueryCondition("CurrentPageIndex").ToString)
            paraPage.PageSize = Convert.ToInt32(QueryCondition("PageSize").ToString)
            paraPage.strSQL = strSQL
            Dim result As New uMESProcessResult

            oResult = dal.RunProcedureForPage(paraPage)

            oResult.Result = True
            Return oResult
        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult
        End Try
    End Function

    ''' <summary>
    ''' 读取产品树的剩余节点
    ''' </summary>
    ''' <returns></returns>
    Private Function LoadProductNodeInfo(ByVal QueryCondition As Dictionary(Of String, String)) As uMESProcessResult Implements uMESDocViewIBusiness.LoadProductNodeInfo
        Dim oResult As New uMESProcessResult

        Try
            Dim strSQL, strProductIDs As String
            strProductIDs = ""
            If QueryCondition.Keys.Contains("ProductIDs") = True Then
                strProductIDs = QueryCondition("ProductIDs").ToString()
            End If

            strSQL = "SELECT  CWorkflow.Productid, CWorkflow.Productname, CWorkflow.Workflowid, CWorkflow.Workflowname, CSpec.Specid," & vbCrLf & _
                    "	      CSpec.Specname, CSpec.Sequence, CWorkflow.ProductFamilyName, CWorkflow.Description AS ProductDesc," & vbCrLf & _
                    "        CWorkflow.Productrevision, CWorkflow.Workflowrevision, CSpec.Specrevision, CWorkflow.WorkflowDesc,CSpec.SpecDesc" & vbCrLf & _
                    "FROM" & vbCrLf & _
                    "(" & vbCrLf & _
                    "SELECT Product.Productid, ProductBase.Productname, Workflow.Workflowid, Workflowbase.Workflowname, Product.Description, " & vbCrLf & _
                    "         ProductFamily.ProductFamilyName,Product.Productrevision, Workflow.Workflowrevision, Workflow.Description AS WorkflowDesc" & vbCrLf & _
                    "FROM Product" & vbCrLf & _
                    "INNER JOIN ProductBase ON Product.Productbaseid = ProductBase.Productbaseid" & vbCrLf & _
                    "INNER JOIN Workflow ON Product.Workflowid = Workflow.Workflowid" & vbCrLf & _
                    "INNER JOIN Workflowbase ON Workflow.Workflowbaseid = Workflowbase.Workflowbaseid" & vbCrLf & _
                    "LEFT OUTER JOIN ProductFamily ON ProductFamily.ProductFamilyid = Product.ProductFamilyid" & vbCrLf & _
                    "WHERE Product.Workflowid <> '0000000000000000' AND Product.Productid IN (" & strProductIDs & ")" & vbCrLf

            strSQL = strSQL & "UNION ALL" & vbCrLf & _
                     "SELECT Product.Productid, ProductBase.Productname, Workflowbase.Revofrcdid, Workflowbase.Workflowname, Product.Description, " & vbCrLf & _
                     "         ProductFamily.ProductFamilyName, Product.Productrevision, Workflow.Workflowrevision, Workflow.Description AS WorkflowDesc" & vbCrLf & _
                     "FROM Product" & vbCrLf & _
                     "INNER JOIN ProductBase ON Product.Productbaseid = ProductBase.Productbaseid" & vbCrLf & _
                     "INNER JOIN Workflowbase ON Product.Workflowbaseid = Workflowbase.Workflowbaseid" & vbCrLf & _
                     "INNER JOIN Workflow ON Workflowbase.Revofrcdid = Workflow.Workflowid" & vbCrLf & _
                     "LEFT OUTER JOIN ProductFamily ON ProductFamily.ProductFamilyid = Product.ProductFamilyid" & vbCrLf & _
                     "WHERE Product.Workflowbaseid  <> '0000000000000000' AND Product.Productid IN (" & strProductIDs & ")" & vbCrLf

            strSQL = strSQL & ") CWorkflow" & vbCrLf & _
                     "INNER JOIN " & vbCrLf & _
                     "(" & vbCrLf & _
                     "SELECT Workflowstep.Workflowid, Spec.Specid, Specbase.Specname, Workflowstep.Sequence, Spec.Specrevision, Spec.Description AS SpecDesc" & vbCrLf & _
                     "FROM Workflowstep" & vbCrLf & _
                     "INNER JOIN Spec ON Workflowstep.Specid = Spec.Specid" & vbCrLf & _
                     "INNER JOIN Specbase ON Spec.Specbaseid = Specbase.Specbaseid" & vbCrLf & _
                     "WHERE Workflowstep.Specid  <> '0000000000000000'" & vbCrLf & _
                     "UNION ALL " & vbCrLf & _
                     "SELECT Workflowstep.Workflowid, Specbase.Revofrcdid,Specbase.Specname, Workflowstep.Sequence, Spec.Specrevision,Spec.Description AS SpecDesc" & vbCrLf & _
                     "FROM Workflowstep" & vbCrLf & _
                     "INNER JOIN Specbase ON Workflowstep.Specbaseid = Specbase.Specbaseid" & vbCrLf & _
                     "INNER JOIN Spec ON Specbase.Revofrcdid = Spec.Specid" & vbCrLf & _
                     "WHERE Workflowstep.Specbaseid  <> '0000000000000000'" & vbCrLf & _
                     "UNION ALL " & vbCrLf & _
                     "SELECT Workflowstep.Workflowid,  Workflow.Workflowid, Workflowbase.Workflowname, Workflowstep.Sequence, Workflow.Workflowrevision, Workflow.Description AS WorkflowDesc" & vbCrLf & _
                     "FROM Workflowstep" & vbCrLf & _
                     "INNER JOIN Workflow ON Workflowstep.Subworkflowid = Workflow.Workflowid" & vbCrLf & _
                     "INNER JOIN Workflowbase ON Workflowbase.Workflowbaseid = Workflow.Workflowbaseid" & vbCrLf & _
                     "WHERE Workflowstep.Subworkflowbaseid = '0000000000000000' " & vbCrLf & _
                     "UNION ALL " & vbCrLf & _
                     "SELECT  Workflowstep.Workflowid, Workflowbase.Revofrcdid, Workflowbase.Workflowname, Workflowstep.Sequence, Workflow.Workflowrevision,  Workflow.Description AS WorkflowDesc" & vbCrLf & _
                     "FROM Workflowstep" & vbCrLf & _
                     "INNER JOIN Workflowbase ON Workflowbase.Workflowbaseid = Workflowstep.Subworkflowbaseid" & vbCrLf & _
                     "INNER JOIN Workflow ON Workflowbase.Revofrcdid = Workflow.Workflowid" & vbCrLf & _
                     "WHERE Workflowstep.Subworkflowid = '0000000000000000' " & vbCrLf & _
                     ") CSpec" & vbCrLf & _
                     "ON CWorkflow.Workflowid = CSpec.Workflowid" & vbCrLf & _
                     "ORDER BY  CWorkflow.Productid, CWorkflow.Workflowid, CSpec.Sequence" & vbCrLf

            Dim dtMain As DataTable = dal.GetQualityRecordInfoForProductNo(strSQL)
            dtMain = DataAdjust.FormatDataSetSpecNameByColumName("SpecName", dtMain)

            oResult.DBTable = dtMain.Copy
            oResult.Result = True
            Return oResult
        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult
        End Try
    End Function

    Private Function GetStepInfo(ByVal specID As String) As DataTable Implements uMESDocViewIBusiness.GetStepInfo
        Dim strSql As String = String.Format("select st.stepinfoname, t.* from stepinfo t left join stepinfobase st on st.stepinfobaseid=t.stepinfobaseid " +
                                            "where t.specid='{0}' order by t.stepseq", specID)
        Dim tmpSet As DataSet = dal.ExecuteQuery(strSql)
        Return tmpSet.Tables(0)
    End Function


    ''' <summary>
    ''' 读取产品文档
    ''' </summary>
    ''' <returns></returns>
    Private Function LoadBOMInfo(ByVal QueryCondition As Dictionary(Of String, String)) As uMESProcessResult Implements uMESDocViewIBusiness.LoadBOMInfo
        Dim oResult As New uMESProcessResult

        Try
            Dim strSQL, strProductID As String
            strProductID = ""
            If QueryCondition.Keys.Contains("ProductID") = True Then
                strProductID = QueryCondition("ProductID").ToString()
            End If

            strSQL = "SELECT CPro.Productid, Productbase.Productname, CChild.Productid AS ProductChildid," & vbCrLf & _
                      "       ProductbaseChild.Productname AS ProductChildname, SUM(CChild.Qtyrequired) AS Qtyrequired" & vbCrLf & _
                      "FROM" & vbCrLf & _
                      "(" & vbCrLf & _
                      "SELECT Product.Productid, Product.Productbaseid, BOM.Bomid" & vbCrLf & _
                      "FROM Product" & vbCrLf & _
                      "INNER JOIN BOM ON Product.Bomid = BOM.Bomid" & vbCrLf & _
                      "WHERE Product.Bombaseid = '0000000000000000' AND Product.Productid = '" & strProductID & "'" & vbCrLf & _
                      "UNION ALL" & vbCrLf & _
                      "SELECT Product.Productid, Product.Productbaseid, BOMBase.Revofrcdid" & vbCrLf & _
                      "FROM Product" & vbCrLf & _
                      "INNER JOIN BOMBase ON Product.Bombaseid = BOMBase.Bombaseid" & vbCrLf & _
                      "WHERE Product.Bomid = '0000000000000000' AND Product.Productid = '" & strProductID & "'" & vbCrLf & _
                      ") CPro" & vbCrLf & _
                      "INNER JOIN" & vbCrLf & _
                      "(" & vbCrLf & _
                      "SELECT Productmateriallistitem.Bomid, ProductChild.Productid, ProductChild.Productbaseid, Productmateriallistitem.Qtyrequired" & vbCrLf & _
                      "FROM Productmateriallistitem " & vbCrLf & _
                      "INNER JOIN Product ProductChild ON ProductChild.Productid = Productmateriallistitem.Productid" & vbCrLf & _
                      "WHERE Productmateriallistitem.Productbaseid = '0000000000000000'" & vbCrLf & _
                      "UNION ALL" & vbCrLf & _
                      "SELECT Productmateriallistitem.Bomid, ProductBaseChild.Revofrcdid, ProductBaseChild.Productbaseid, Productmateriallistitem.Qtyrequired" & vbCrLf & _
                      "FROM Productmateriallistitem " & vbCrLf & _
                      "INNER JOIN ProductBase ProductBaseChild ON ProductBaseChild.Productbaseid = Productmateriallistitem.Productbaseid" & vbCrLf & _
                      "WHERE Productmateriallistitem.Productid = '0000000000000000'" & vbCrLf & _
                      ") CChild" & vbCrLf & _
                      "ON CPro.Bomid = CChild.Bomid" & vbCrLf & _
                      "INNER JOIN Productbase ON Productbase.Productbaseid = CPro.Productbaseid" & vbCrLf & _
                      "INNER JOIN Productbase ProductbaseChild ON ProductbaseChild.Productbaseid = CChild.Productbaseid" & vbCrLf & _
                      "GROUP BY CPro.Productid, Productbase.Productname, CChild.Productid, ProductbaseChild.Productname " & vbCrLf & _
                      "ORDER BY Productbase.Productname,  ProductbaseChild.Productname " & vbCrLf

            Dim dtMain As DataTable = dal.GetQualityRecordInfoForProductNo(strSQL)
            oResult.DBTable = dtMain.Copy
            oResult.Result = True
            Return oResult
        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult
        End Try
    End Function

    ''' <summary>
    ''' 读取产品文档
    ''' </summary>
    ''' <returns></returns>
    Private Function LoadProductDocInfo(ByVal QueryCondition As Dictionary(Of String, String)) As uMESProcessResult Implements uMESDocViewIBusiness.LoadProductDocInfo
        Dim oResult As New uMESProcessResult

        Try
            Dim strSQL, strProductID As String
            strProductID = ""
            If QueryCondition.Keys.Contains("ProductID") = True Then
                strProductID = QueryCondition("ProductID").ToString()
            End If

            strSQL = "SELECT DISTINCT '' AS Filename, Cinfo.Filepath,Cinfo.Documentrevision,Cinfo.Documentname,Cinfo.Documentid,Cinfo.Documentbaseid" & vbCrLf & _
                    "FROM " & vbCrLf & _
                    "(" & vbCrLf & _
                    "SELECT Document.Identifier AS FilePath,Document.Documentrevision,Documentbase.Documentname,Documententry.Documentid,Documententry.Documentbaseid  " & vbCrLf & _
                    "FROM Product " & vbCrLf & _
                    "INNER JOIN Documentset ON Product.Documentsetid = Documentset.Documentsetid " & vbCrLf & _
                    "INNER JOIN Documententry ON Documentset.Documentsetid = Documententry.Documentsetid " & vbCrLf & _
                    "INNER JOIN Document ON Documententry.Documentid = Document.Documentid " & vbCrLf & _
                    "INNER JOIN Documentbase ON Document.Documentbaseid=Documentbase.Documentbaseid " & vbCrLf & _
                    "WHERE Document.Documentid <> '0000000000000000' AND Document.Identifier IS NOT NULL AND Product.ProductID='" & strProductID & "'" & vbCrLf

            If QueryCondition.Keys.Contains("SecretLevel") = True Then
                strSQL += "AND (Document.Secretlevel <= " & QueryCondition("SecretLevel").ToString & " OR Document.Secretlevel IS NULL)" & vbCrLf
            End If

            strSQL += "UNION ALL " & vbCrLf & _
                    "SELECT Document.Identifier AS FilePath,Document.Documentrevision,Documentbase.Documentname,Documententry.Documentid,Documententry.Documentbaseid " & vbCrLf & _
                    "FROM Product " & vbCrLf & _
                    "INNER JOIN Documentset ON Product.Documentsetid = Documentset.Documentsetid " & vbCrLf & _
                    "INNER JOIN Documententry ON Documentset.Documentsetid = Documententry.Documentsetid " & vbCrLf & _
                    "INNER JOIN Documentbase ON Documententry.Documentbaseid = Documentbase.Documentbaseid " & vbCrLf & _
                    "INNER JOIN Document ON Document.Documentid = Documentbase.Revofrcdid " & vbCrLf & _
                    "WHERE Document.Documentbaseid <> '0000000000000000' AND Document.Identifier IS NOT NULL AND Product.ProductID='" & strProductID & "'" & vbCrLf

            If QueryCondition.Keys.Contains("SecretLevel") = True Then
                strSQL += "AND (Document.Secretlevel <= " & QueryCondition("SecretLevel").ToString & " OR Document.Secretlevel IS NULL)" & vbCrLf
            End If

            strSQL += ") Cinfo" & vbCrLf & _
                    "ORDER BY Cinfo.Documentname " & vbCrLf

            Dim dtMain As DataTable = dal.GetQualityRecordInfoForProductNo(strSQL)
            dtMain.Columns.Add("IsRevOfRcd")
            If dtMain.Rows.Count > 0 Then

                For Each row As DataRow In dtMain.Rows
                    Dim strName = row("Filepath").ToString().Trim()

                    If strName.IndexOf("http") <> -1 Then
                        Dim intIndex1 As Integer = strName.LastIndexOf("/")
                        row("Filename") = strName.Substring(intIndex1 + 1)
                    Else
                        Dim intFlag As Integer = strName.LastIndexOf("\")
                        row("Filename") = strName.Substring(intFlag + 1)
                    End If
                    If row("Documentid") = "0000000000000000" Then
                        row("IsRevOfRcd") = False
                    Else
                        row("IsRevOfRcd") = True
                    End If
                Next
                dtMain.AcceptChanges()
            End If
            oResult.DBTable = dtMain.Copy
            oResult.Result = True
            Return oResult
        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult
        End Try
    End Function

    ''' <summary>
    ''' 读取流程文档
    ''' </summary>
    ''' <returns></returns>
    Private Function LoadWorkflowDocInfo(ByVal QueryCondition As Dictionary(Of String, String)) As uMESProcessResult Implements uMESDocViewIBusiness.LoadWorkflowDocInfo
        Dim oResult As New uMESProcessResult
        Dim strSQL, strWorkflowID As String
        strWorkflowID = ""
        If QueryCondition.Keys.Contains("WorkflowID") = True Then
            strWorkflowID = QueryCondition("WorkflowID").ToString()
        End If
        Try
            strSQL = "SELECT DISTINCT '' AS Filename, Cinfo.Filepath,Cinfo.Documentrevision,Cinfo.Documentname,Cinfo.Documentid,Cinfo.Documentbaseid" & vbCrLf & _
                           "FROM " & vbCrLf & _
                           "(" & vbCrLf & _
                           "SELECT Document.Identifier AS FilePath,Document.Documentrevision,Documentbase.Documentname,Documententry.Documentid,Documententry.Documentbaseid  " & vbCrLf & _
                           "FROM Workflow " & vbCrLf & _
                           "INNER JOIN Documentset ON Workflow.Documentsetid = Documentset.Documentsetid " & vbCrLf & _
                           "INNER JOIN Documententry ON Documentset.Documentsetid = Documententry.Documentsetid " & vbCrLf & _
                           "INNER JOIN Document ON Documententry.Documentid = Document.Documentid " & vbCrLf & _
                           "INNER JOIN Documentbase ON Document.Documentbaseid=Documentbase.Documentbaseid " & vbCrLf & _
                           "WHERE Document.Documentid <> '0000000000000000' AND Document.Identifier IS NOT NULL AND Workflow.WorkflowID='" & strWorkflowID & "'" & vbCrLf

            If QueryCondition.Keys.Contains("SecretLevel") = True Then
                strSQL += "AND (Document.Secretlevel <= " & QueryCondition("SecretLevel").ToString & " OR Document.Secretlevel IS NULL)" & vbCrLf
            End If

            strSQL += "UNION ALL " & vbCrLf & _
                           "SELECT Document.Identifier AS FilePath,Document.Documentrevision,Documentbase.Documentname,Documententry.Documentid,Documententry.Documentbaseid " & vbCrLf & _
                           "FROM Workflow " & vbCrLf & _
                           "INNER JOIN Documentset ON Workflow.Documentsetid = Documentset.Documentsetid " & vbCrLf & _
                           "INNER JOIN Documententry ON Documentset.Documentsetid = Documententry.Documentsetid " & vbCrLf & _
                           "INNER JOIN Documentbase ON Documententry.Documentbaseid = Documentbase.Documentbaseid " & vbCrLf & _
                           "INNER JOIN Document ON Document.Documentid = Documentbase.Revofrcdid " & vbCrLf & _
                           "WHERE Document.Documentbaseid <> '0000000000000000' AND Document.Identifier IS NOT NULL AND Workflow.WorkflowID='" & strWorkflowID & "'" & vbCrLf

            If QueryCondition.Keys.Contains("SecretLevel") = True Then
                strSQL += "AND (Document.Secretlevel <= " & QueryCondition("SecretLevel").ToString & " OR Document.Secretlevel IS NULL)" & vbCrLf
            End If

            strSQL += ") Cinfo" & vbCrLf & _
                           "ORDER BY Cinfo.Documentname " & vbCrLf

            Dim dtMain As DataTable = dal.GetQualityRecordInfoForProductNo(strSQL)
            dtMain.Columns.Add("IsRevOfRcd")
            If dtMain.Rows.Count > 0 Then

                For Each row As DataRow In dtMain.Rows
                    Dim strName = row("Filepath").ToString().Trim()

                    If strName.IndexOf("http") <> -1 Then
                        Dim intIndex1 As Integer = strName.LastIndexOf("/")
                        row("Filename") = strName.Substring(intIndex1 + 1)
                    Else
                        Dim intFlag As Integer = strName.LastIndexOf("\")
                        row("Filename") = strName.Substring(intFlag + 1)
                    End If
                    If row("Documentid") = "0000000000000000" Then
                        row("IsRevOfRcd") = False
                    Else
                        row("IsRevOfRcd") = True
                    End If
                Next
                dtMain.AcceptChanges()
            End If
            oResult.DBTable = dtMain.Copy
            oResult.Result = True
            Return oResult
        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult
        End Try
    End Function

    ''' <summary>
    ''' 读取工序文档
    ''' </summary>
    ''' <returns></returns>
    Private Function LoadSpecDocInfo(ByVal QueryCondition As Dictionary(Of String, String)) As uMESProcessResult Implements uMESDocViewIBusiness.LoadSpecDocInfo
        Dim oResult As New uMESProcessResult
        Dim strSQL, strSpecID As String
        strSpecID = ""
        If QueryCondition.Keys.Contains("SpecID") = True Then
            strSpecID = QueryCondition("SpecID").ToString()
        End If
        Try
            strSQL = "SELECT DISTINCT '' AS Filename, Cinfo.Filepath,Cinfo.Documentrevision,Cinfo.Documentname,Cinfo.Documentid,Cinfo.Documentbaseid" & vbCrLf & _
                      "FROM " & vbCrLf & _
                      "(" & vbCrLf & _
                      "SELECT Spec.Specid, Document.Identifier AS FilePath,Document.Documentrevision,Documentbase.Documentname,Documententry.Documentid,Documententry.Documentbaseid  " & vbCrLf & _
                      "FROM Spec " & vbCrLf & _
                      "INNER JOIN Documentset ON Spec.Documentsetid = Documentset.Documentsetid " & vbCrLf & _
                      "INNER JOIN Documententry ON Documentset.Documentsetid = Documententry.Documentsetid " & vbCrLf & _
                      "INNER JOIN Document ON Documententry.Documentid = Document.Documentid " & vbCrLf & _
                      "INNER JOIN Documentbase ON Document.Documentbaseid=Documentbase.Documentbaseid " & vbCrLf & _
                      "WHERE Document.Documentid <> '0000000000000000' AND Document.Identifier IS NOT NULL AND Spec.SpecID='" & strSpecID & "'" & vbCrLf & _
                      "AND Spec.Specid ='" & strSpecID & "'" & vbCrLf

            If QueryCondition.Keys.Contains("SecretLevel") = True Then
                strSQL += "AND (Document.Secretlevel <= " & QueryCondition("SecretLevel").ToString & " OR Document.Secretlevel IS NULL)" & vbCrLf
            End If

            strSQL += "UNION ALL " & vbCrLf & _
                      "SELECT Spec.Specid, Document.Identifier AS FilePath,Document.Documentrevision,Documentbase.Documentname,Documententry.Documentid,Documententry.Documentbaseid " & vbCrLf & _
                      "FROM Spec " & vbCrLf & _
                      "INNER JOIN Documentset ON Spec.Documentsetid = Documentset.Documentsetid " & vbCrLf & _
                      "INNER JOIN Documententry ON Documentset.Documentsetid = Documententry.Documentsetid " & vbCrLf & _
                      "INNER JOIN Documentbase ON Documententry.Documentbaseid = Documentbase.Documentbaseid " & vbCrLf & _
                      "INNER JOIN Document ON Document.Documentid = Documentbase.Revofrcdid " & vbCrLf & _
                      "WHERE Document.Documentbaseid <> '0000000000000000' AND Document.Identifier IS NOT NULL AND Spec.SpecID='" & strSpecID & "'" & vbCrLf & _
                      "AND Spec.Specid ='" & strSpecID & "'" & vbCrLf

            If QueryCondition.Keys.Contains("SecretLevel") = True Then
                strSQL += "AND (Document.Secretlevel <= " & QueryCondition("SecretLevel").ToString & " OR Document.Secretlevel IS NULL)" & vbCrLf
            End If

            strSQL += ") Cinfo" & vbCrLf & _
                      "ORDER BY Cinfo.Documentname " & vbCrLf

            Dim dtMain As DataTable = dal.GetQualityRecordInfoForProductNo(strSQL)
            dtMain.Columns.Add("IsRevOfRcd")
            If dtMain.Rows.Count > 0 Then

                For Each row As DataRow In dtMain.Rows
                    Dim strName = row("Filepath").ToString().Trim()

                    If strName.IndexOf("http") <> -1 Then
                        Dim intIndex1 As Integer = strName.LastIndexOf("/")
                        row("Filename") = strName.Substring(intIndex1 + 1)
                    Else
                        Dim intFlag As Integer = strName.LastIndexOf("\")
                        row("Filename") = strName.Substring(intFlag + 1)
                    End If
                    If row("Documentid") = "0000000000000000" Then
                        row("IsRevOfRcd") = False
                    Else
                        row("IsRevOfRcd") = True
                    End If
                Next
                dtMain.AcceptChanges()
            End If
            oResult.DBTable = dtMain.Copy
            oResult.Result = True
            Return oResult
        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult
        End Try
    End Function

    ''' <summary>
    ''' 读取工步文档
    ''' </summary>
    ''' <returns></returns>
    Private Function LoadStepDocInfo(ByVal QueryCondition As Dictionary(Of String, String)) As uMESProcessResult Implements uMESDocViewIBusiness.LoadStepDocInfo
        Dim oResult As New uMESProcessResult
        Dim strSQL, strStepID As String
        strStepID = ""

        If QueryCondition.Keys.Contains("StepID") = True Then
            strStepID = QueryCondition("StepID").ToString()
        End If
        Try
            strSQL = "SELECT DISTINCT '' AS Filename, Cinfo.Filepath,Cinfo.Documentrevision,Cinfo.Documentname,Cinfo.Documentid,Cinfo.Documentbaseid" & vbCrLf & _
                      "FROM " & vbCrLf & _
                      "(" & vbCrLf & _
                      "SELECT stepinfo.stepinfoid, Document.Identifier AS FilePath,Document.Documentrevision,Documentbase.Documentname,Documententry.Documentid,Documententry.Documentbaseid  " & vbCrLf & _
                      "FROM stepinfo " & vbCrLf & _
                      "INNER JOIN Documentset ON stepinfo.Documentsetid = Documentset.Documentsetid " & vbCrLf & _
                      "INNER JOIN Documententry ON Documentset.Documentsetid = Documententry.Documentsetid " & vbCrLf & _
                      "INNER JOIN Document ON Documententry.Documentid = Document.Documentid " & vbCrLf & _
                      "INNER JOIN Documentbase ON Document.Documentbaseid=Documentbase.Documentbaseid " & vbCrLf & _
                      "WHERE Document.Documentid <> '0000000000000000' AND Document.Identifier IS NOT NULL AND stepinfo.stepinfoid='" & strStepID & "'" & vbCrLf & _
                      "AND stepinfo.stepinfoid ='" & strStepID & "'" & vbCrLf

            'If QueryCondition.Keys.Contains("SecretLevel") = True Then
            '    strSQL += "AND (Document.Secretlevel <= " & QueryCondition("SecretLevel").ToString & " OR Document.Secretlevel IS NULL)" & vbCrLf
            'End If

            strSQL += "UNION ALL " & vbCrLf & _
                      "SELECT stepinfo.stepinfoid, Document.Identifier AS FilePath,Document.Documentrevision,Documentbase.Documentname,Documententry.Documentid,Documententry.Documentbaseid " & vbCrLf & _
                      "FROM stepinfo " & vbCrLf & _
                      "INNER JOIN Documentset ON stepinfo.Documentsetid = Documentset.Documentsetid " & vbCrLf & _
                      "INNER JOIN Documententry ON Documentset.Documentsetid = Documententry.Documentsetid " & vbCrLf & _
                      "INNER JOIN Documentbase ON Documententry.Documentbaseid = Documentbase.Documentbaseid " & vbCrLf & _
                      "INNER JOIN Document ON Document.Documentid = Documentbase.Revofrcdid " & vbCrLf & _
                      "WHERE Document.Documentbaseid <> '0000000000000000' AND Document.Identifier IS NOT NULL AND stepinfo.stepinfoid='" & strStepID & "'" & vbCrLf & _
                      "AND  stepinfo.stepinfoid ='" & strStepID & "'" & vbCrLf

            'If QueryCondition.Keys.Contains("SecretLevel") = True Then
            '    strSQL += "AND (Document.Secretlevel <= " & QueryCondition("SecretLevel").ToString & " OR Document.Secretlevel IS NULL)" & vbCrLf
            'End If

            strSQL += ") Cinfo" & vbCrLf & _
                      "ORDER BY Cinfo.Documentname " & vbCrLf

            Dim dtMain As DataTable = dal.GetQualityRecordInfoForProductNo(strSQL)
            dtMain.Columns.Add("IsRevOfRcd")
            If dtMain.Rows.Count > 0 Then

                For Each row As DataRow In dtMain.Rows
                    Dim strName = row("Filepath").ToString().Trim()

                    If strName.IndexOf("http") <> -1 Then
                        Dim intIndex1 As Integer = strName.LastIndexOf("/")
                        row("Filename") = strName.Substring(intIndex1 + 1)
                    Else
                        Dim intFlag As Integer = strName.LastIndexOf("\")
                        row("Filename") = strName.Substring(intFlag + 1)
                    End If
                    If row("Documentid") = "0000000000000000" Then
                        row("IsRevOfRcd") = False
                    Else
                        row("IsRevOfRcd") = True
                    End If
                Next
                dtMain.AcceptChanges()
            End If
            oResult.DBTable = dtMain.Copy
            oResult.Result = True
            Return oResult
        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult
        End Try
    End Function



    ''' <summary>
    ''' 读取专用工装文档
    ''' </summary>
    ''' <returns></returns>
    Private Function LoadSpecialToolDocInfo(ByVal QueryCondition As Dictionary(Of String, String)) As uMESProcessResult Implements uMESDocViewIBusiness.LoadSpecialToolDocInfo
        Dim oResult As New uMESProcessResult
        Dim strSQL, strSpecialToolID As String
        strSpecialToolID = ""
        If QueryCondition.Keys.Contains("SpecialToolID") = True Then
            strSpecialToolID = QueryCondition("SpecialToolID").ToString()
        End If
        Try
            strSQL = "SELECT DISTINCT '' AS Filename, Cinfo.Filepath,Cinfo.Documentrevision,Cinfo.Documentname,Cinfo.Documentid,Cinfo.Documentbaseid" & vbCrLf & _
                     "FROM " & vbCrLf & _
                     "(" & vbCrLf & _
                     "SELECT XASpecialTool.XASpecialToolID, Document.Identifier AS FilePath,Document.Documentrevision,Documentbase.Documentname,Documententry.Documentid,Documententry.Documentbaseid  " & vbCrLf & _
                     "FROM XASpecialTool " & vbCrLf & _
                     "INNER JOIN Documentset ON XASpecialTool.Documentsetid = Documentset.Documentsetid " & vbCrLf & _
                     "INNER JOIN Documententry ON Documentset.Documentsetid = Documententry.Documentsetid " & vbCrLf & _
                     "INNER JOIN Document ON Documententry.Documentid = Document.Documentid " & vbCrLf & _
                     "INNER JOIN Documentbase ON Document.Documentbaseid=Documentbase.Documentbaseid " & vbCrLf & _
                     "WHERE Document.Documentid <> '0000000000000000' AND Document.Identifier IS NOT NULL " & vbCrLf & _
                     "AND XASpecialTool.XASpecialToolid ='" & strSpecialToolID & "'" & vbCrLf

            If QueryCondition.Keys.Contains("SecretLevel") = True Then
                strSQL += "AND (Document.Secretlevel <= " & QueryCondition("SecretLevel").ToString & " OR Document.Secretlevel IS NULL)" & vbCrLf
            End If

            strSQL += "UNION ALL " & vbCrLf & _
                     "SELECT XASpecialTool.XASpecialToolid, Document.Identifier AS FilePath,Document.Documentrevision,Documentbase.Documentname,Documententry.Documentid,Documententry.Documentbaseid " & vbCrLf & _
                     "FROM XASpecialTool " & vbCrLf & _
                     "INNER JOIN Documentset ON XASpecialTool.Documentsetid = Documentset.Documentsetid " & vbCrLf & _
                     "INNER JOIN Documententry ON Documentset.Documentsetid = Documententry.Documentsetid " & vbCrLf & _
                     "INNER JOIN Documentbase ON Documententry.Documentbaseid = Documentbase.Documentbaseid " & vbCrLf & _
                     "INNER JOIN Document ON Document.Documentid = Documentbase.Revofrcdid " & vbCrLf & _
                     "WHERE Document.Documentbaseid <> '0000000000000000' AND Document.Identifier IS NOT NULL " & vbCrLf & _
                     "AND XASpecialTool.XASpecialToolid ='" & strSpecialToolID & "'" & vbCrLf

            If QueryCondition.Keys.Contains("SecretLevel") = True Then
                strSQL += "AND (Document.Secretlevel <= " & QueryCondition("SecretLevel").ToString & " OR Document.Secretlevel IS NULL)" & vbCrLf
            End If

            strSQL += ") Cinfo" & vbCrLf & _
                     "ORDER BY Cinfo.Documentname " & vbCrLf

            Dim dtMain As DataTable = dal.GetQualityRecordInfoForProductNo(strSQL)
            dtMain.Columns.Add("IsRevOfRcd")
            If dtMain.Rows.Count > 0 Then

                For Each row As DataRow In dtMain.Rows
                    Dim strName = row("Filepath").ToString().Trim()

                    If strName.IndexOf("http") <> -1 Then
                        Dim intIndex1 As Integer = strName.LastIndexOf("/")
                        row("Filename") = strName.Substring(intIndex1 + 1)
                    Else
                        Dim intFlag As Integer = strName.LastIndexOf("\")
                        row("Filename") = strName.Substring(intFlag + 1)
                    End If
                    If row("Documentid") = "0000000000000000" Then
                        row("IsRevOfRcd") = False
                    Else
                        row("IsRevOfRcd") = True
                    End If
                Next
                dtMain.AcceptChanges()
            End If
            oResult.DBTable = dtMain.Copy
            oResult.Result = True
            Return oResult
        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult
        End Try
    End Function

    ''' <summary>
    ''' 读取通用工装文档
    ''' </summary>
    ''' <returns></returns>
    Private Function LoadCommToolDocInfo(ByVal QueryCondition As Dictionary(Of String, String)) As uMESProcessResult Implements uMESDocViewIBusiness.LoadCommToolDocInfo
        Dim oResult As New uMESProcessResult
        Dim strSQL, strCommToolID As String
        strCommToolID = ""
        If QueryCondition.Keys.Contains("CommToolID") = True Then
            strCommToolID = QueryCondition("CommToolID").ToString()
        End If
        Try
            strSQL = "SELECT Rownum rn, t.* from ( SELECT DISTINCT '' AS Filename, Cinfo.Filepath,Cinfo.Documentrevision,Cinfo.Documentname,Cinfo.Documentid,Cinfo.Documentbaseid" & vbCrLf & _
                     "FROM " & vbCrLf & _
                     "(" & vbCrLf & _
                     "SELECT Xacommtool.XacommtoolID, Document.Identifier AS FilePath,Document.Documentrevision,Documentbase.Documentname,Documententry.Documentid,Documententry.Documentbaseid  " & vbCrLf & _
                     "FROM Xacommtool " & vbCrLf & _
                     "INNER JOIN Documentset ON Xacommtool.Documentsetid = Documentset.Documentsetid " & vbCrLf & _
                     "INNER JOIN Documententry ON Documentset.Documentsetid = Documententry.Documentsetid " & vbCrLf & _
                     "INNER JOIN Document ON Documententry.Documentid = Document.Documentid " & vbCrLf & _
                     "INNER JOIN Documentbase ON Document.Documentbaseid=Documentbase.Documentbaseid " & vbCrLf & _
                     "WHERE Document.Documentid <> '0000000000000000' AND Document.Identifier IS NOT NULL AND Xacommtool.XacommtoolID='" & strCommToolID & "'" & vbCrLf

            If QueryCondition.Keys.Contains("SecretLevel") = True Then
                strSQL += "AND (Document.Secretlevel <= " & QueryCondition("SecretLevel").ToString & " OR Document.Secretlevel IS NULL)" & vbCrLf
            End If

            strSQL += "UNION ALL " & vbCrLf & _
                     "SELECT Xacommtool.XacommtoolID, Document.Identifier AS FilePath,Document.Documentrevision,Documentbase.Documentname,Documententry.Documentid,Documententry.Documentbaseid " & vbCrLf & _
                     "FROM Xacommtool " & vbCrLf & _
                     "INNER JOIN Documentset ON Xacommtool.Documentsetid = Documentset.Documentsetid " & vbCrLf & _
                     "INNER JOIN Documententry ON Documentset.Documentsetid = Documententry.Documentsetid " & vbCrLf & _
                     "INNER JOIN Documentbase ON Documententry.Documentbaseid = Documentbase.Documentbaseid " & vbCrLf & _
                     "INNER JOIN Document ON Document.Documentid = Documentbase.Revofrcdid " & vbCrLf & _
                     "WHERE Document.Documentbaseid <> '0000000000000000' AND Document.Identifier IS NOT NULL AND Xacommtool.XacommtoolID='" & strCommToolID & "'" & vbCrLf

            If QueryCondition.Keys.Contains("SecretLevel") = True Then
                strSQL += "AND (Document.Secretlevel <= " & QueryCondition("SecretLevel").ToString & " OR Document.Secretlevel IS NULL)" & vbCrLf
            End If

            strSQL += ") Cinfo" & vbCrLf & _
                     "ORDER BY Cinfo.Documentname ) t " & vbCrLf

            Dim dtMain As DataTable = dal.GetQualityRecordInfoForProductNo(strSQL)
            dtMain.Columns.Add("IsRevOfRcd")
            If dtMain.Rows.Count > 0 Then

                For Each row As DataRow In dtMain.Rows
                    Dim strName = row("Filepath").ToString().Trim()

                    If strName.IndexOf("http") <> -1 Then
                        Dim intIndex1 As Integer = strName.LastIndexOf("/")
                        row("Filename") = strName.Substring(intIndex1 + 1)
                    Else
                        Dim intFlag As Integer = strName.LastIndexOf("\")
                        row("Filename") = strName.Substring(intFlag + 1)
                    End If
                    If row("Documentid") = "0000000000000000" Then
                        row("IsRevOfRcd") = False
                    Else
                        row("IsRevOfRcd") = True
                    End If
                Next
                dtMain.AcceptChanges()
            End If
            oResult.DBTable = dtMain.Copy
            oResult.Result = True
            Return oResult
        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult
        End Try
    End Function

    ''' <summary>
    ''' 读取产品信息
    ''' </summary>
    ''' <returns></returns>
    Private Function LoadProductInfo(ByVal QueryCondition As Dictionary(Of String, String)) As uMESProcessResult Implements uMESDocViewIBusiness.LoadProductInfo
        Dim oResult As New uMESProcessResult

        Try
            Dim strSQL, strProductID As String
            strProductID = ""
            If QueryCondition.Keys.Contains("ProductID") = True Then
                strProductID = QueryCondition("ProductID").ToString()
            End If
            strSQL = "SELECT Productbase.Productname, Product.Productrevision, Product.Description, " & vbCrLf & _
                     "       Productfamily.Productfamilyname, Producttype.Producttypename, Phasemark.Phasemarkname" & vbCrLf & _
                     "FROM Product" & vbCrLf & _
                     "INNER JOIN Productbase ON Product.Productbaseid = Productbase.Productbaseid" & vbCrLf & _
                     "LEFT OUTER JOIN Productfamily ON Product.Productfamilyid = Productfamily.Productfamilyid" & vbCrLf & _
                     "LEFT OUTER JOIN Producttype ON Producttype.Producttypeid = Product.Producttypeid" & vbCrLf & _
                     "LEFT OUTER JOIN Phasemark ON Product.Phasemarkid = Phasemark.Phasemarkid" & vbCrLf & _
                     "WHERE Product.ProductID ='" & strProductID & "'" & vbCrLf

            Dim dtMain As DataTable = dal.GetQualityRecordInfoForProductNo(strSQL)
            oResult.DBTable = dtMain.Copy
            oResult.Result = True
            Return oResult
        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult
        End Try
    End Function

    ''' <summary>
    ''' 读取工艺流程信息
    ''' </summary>
    ''' <returns></returns>
    Private Function LoadWorkflowInfo(ByVal QueryCondition As Dictionary(Of String, String)) As uMESProcessResult Implements uMESDocViewIBusiness.LoadWorkflowInfo
        Dim oResult As New uMESProcessResult

        Try
            Dim strSQL, strWorkflowID As String
            strWorkflowID = ""
            If QueryCondition.Keys.Contains("WorkflowID") = True Then
                strWorkflowID = QueryCondition("WorkflowID").ToString()
            End If
            strSQL = "SELECT Workflowbase.Workflowname, Workflow.Workflowrevision,Workflow.Description" & vbCrLf & _
                   "FROM Workflow" & vbCrLf & _
                   "INNER JOIN Workflowbase ON Workflow.Workflowbaseid = Workflowbase.Workflowbaseid" & vbCrLf & _
                   "WHERE Workflow.WorkflowID ='" & strWorkflowID & "'" & vbCrLf

            Dim dtMain As DataTable = dal.GetQualityRecordInfoForProductNo(strSQL)
            oResult.DBTable = dtMain.Copy
            oResult.Result = True
            Return oResult
        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult
        End Try
    End Function

    ''' <summary>
    ''' 读取工序信息
    ''' </summary>
    ''' <returns></returns>
    Private Function LoadSpecInfo(ByVal QueryCondition As Dictionary(Of String, String)) As uMESProcessResult Implements uMESDocViewIBusiness.LoadSpecInfo
        Dim oResult As New uMESProcessResult

        Try
            Dim strSQL, strSpecID As String
            strSpecID = ""
            If QueryCondition.Keys.Contains("SpecID") = True Then
                strSpecID = QueryCondition("SpecID").ToString()
            End If
            strSQL = "SELECT Specbase.Specname, Spec.Specrevision, Spec.Specno," & vbCrLf & _
                      "       Resourcegroup.Resourcegroupname, Spec.Opdesc" & vbCrLf & _
                      "FROM Spec" & vbCrLf & _
                      "INNER JOIN Specbase ON Spec.Specbaseid = Specbase.Specbaseid" & vbCrLf & _
                      "LEFT OUTER JOIN Resourcegroup ON Spec.Resourcegroupid = Resourcegroup.Resourcegroupid" & vbCrLf & _
                      "WHERE Spec.Specid = '" & strSpecID & "'" & vbCrLf

            Dim dt As DataTable = dal.GetQualityRecordInfoForProductNo(strSQL)
            dt = DataAdjust.FormatDataSetSpecNameByColumName("Specname", dt)
            If dt.Rows.Count > 0 Then
                For i As Integer = 0 To dt.Rows.Count - 1
                    For j As Integer = 0 To dt.Columns.Count - 1
                        Select Case dt.Columns(j).ColumnName
                            Case "Specname"
                                If dt.Rows(i).IsNull("Specname") = False Then
                                    dt.Rows(i).Item("Specname") = DataAdjust.GetSpecNameWithOutProductNo(dt.Rows(i).Item("Specname")).ToString().Trim()
                                End If
                            Case "Isgjgx"
                                If dt.Rows(i).IsNull("Isgjgx") = False Then
                                    If dt.Rows(i).Item("Isgjgx") = 1 Then
                                        dt.Rows(i).Item("Isgjgx") = "是"
                                    Else
                                        dt.Rows(i).Item("Isgjgx") = "否"
                                    End If
                                Else
                                    dt.Rows(i).Item("Isgjgx") = "否"
                                End If
                            Case "Ckiszggx"
                                If dt.Rows(i).IsNull("Ckiszggx") = False Then
                                    If dt.Rows(i).Item("Ckiszggx") = 1 Then
                                        dt.Rows(i).Item("Ckiszggx") = "是"
                                    Else
                                        dt.Rows(i).Item("Ckiszggx") = "否"
                                    End If
                                Else
                                    dt.Rows(i).Item("Ckiszggx") = "否"
                                End If
                            Case "Isncspec"
                                If dt.Rows(i).IsNull("Isncspec") = False Then
                                    If dt.Rows(i).Item("Isncspec") = 1 Then
                                        dt.Rows(i).Item("Isncspec") = "是"
                                    Else
                                        dt.Rows(i).Item("Isncspec") = "否"
                                    End If
                                Else
                                    dt.Rows(i).Item("Isncspec") = "否"
                                End If
                        End Select
                    Next
                Next
            End If

            oResult.DBTable = dt.Copy
            oResult.Result = True
            Return oResult
        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult
        End Try
    End Function

    ''' <summary>
    ''' 读取设备信息
    ''' </summary>
    ''' <returns></returns>
    Private Function LoadResourceInfo(ByVal QueryCondition As Dictionary(Of String, String)) As uMESProcessResult Implements uMESDocViewIBusiness.LoadResourceInfo
        Dim oResult As New uMESProcessResult

        Try
            Dim strSQL, strResourceID As String
            strResourceID = ""
            If QueryCondition.Keys.Contains("ResourceID") = True Then
                strResourceID = QueryCondition("ResourceID").ToString()
            End If
            strSQL = "SELECT Resourcedef.Resourcename, Resourcedef.Description,Resourcetype.Resourcetypename, Resourcedef.ResourceNum" & vbCrLf & _
             "FROM Resourcedef" & vbCrLf & _
             "LEFT OUTER JOIN Resourcetype ON Resourcetype.Resourcetypeid = Resourcedef.Resourcetypeid" & vbCrLf & _
             "WHERE Resourcedef.ResourceID ='" & strResourceID & "'" & vbCrLf

            Dim dtMain As DataTable = dal.GetQualityRecordInfoForProductNo(strSQL)
            oResult.DBTable = dtMain.Copy
            oResult.Result = True
            Return oResult
        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult
        End Try
    End Function


    ''' <summary>
    ''' 读取工序上的设备列表
    ''' </summary>
    ''' <returns></returns>
    Private Function LoadSpecResource(ByVal QueryCondition As Dictionary(Of String, String)) As uMESProcessResult Implements uMESDocViewIBusiness.LoadSpecResource
        Dim oResult As New uMESProcessResult

        Try
            Dim strSQL, strSpecID As String
            strSpecID = ""
            If QueryCondition.Keys.Contains("SpecID") = True Then
                strSpecID = QueryCondition("SpecID").ToString()
            End If
            strSQL = "SELECT Resourcedef.ResourceID,Resourcedef.Resourcename,Resourcedef.Description" & vbCrLf & _
                     "FROM Spec" & vbCrLf & _
                     "INNER JOIN Resourcegroup ON Spec.Resourcegroupid = Resourcegroup.Resourcegroupid" & vbCrLf & _
                     "INNER JOIN Resourcegroupentries ON Resourcegroupentries.Resourcegroupid = Resourcegroup.Resourcegroupid" & vbCrLf & _
                     "INNER JOIN Resourcedef ON Resourcedef.Resourceid = Resourcegroupentries.Entriesid" & vbCrLf & _
                     "WHERE Spec.SpecID ='" & strSpecID & "'" & vbCrLf & _
                     "ORDER BY Resourcedef.Resourcename" & vbCrLf

            Dim dtMain As DataTable = dal.GetQualityRecordInfoForProductNo(strSQL)
            oResult.DBTable = dtMain.Copy
            oResult.Result = True
            Return oResult
        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult
        End Try
    End Function

    ''' <summary>
    ''' 读取工序上的专用工装列表
    ''' </summary>
    ''' <returns></returns>
    Private Function LoadSpecSpecialTool(ByVal QueryCondition As Dictionary(Of String, String)) As uMESProcessResult Implements uMESDocViewIBusiness.LoadSpecSpecialTool
        Dim oResult As New uMESProcessResult

        Try
            Dim strSQL, strSpecID As String
            strSpecID = ""
            If QueryCondition.Keys.Contains("SpecID") = True Then
                strSpecID = QueryCondition("SpecID").ToString()
            End If
            strSQL = "SELECT XASpecialTool.XASpecialToolID,XASpecialTool.Xaspecialtoolname, XASpecialTool.Description " & vbCrLf & _
                       "FROM XASpecialTool" & vbCrLf & _
                       "WHERE XASpecialTool.SpecID ='" & strSpecID & "'" & vbCrLf & _
                       "ORDER BY XASpecialTool.Xaspecialtoolname" & vbCrLf

            Dim dtMain As DataTable = dal.GetQualityRecordInfoForProductNo(strSQL)
            oResult.DBTable = dtMain.Copy
            oResult.Result = True
            Return oResult
        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult
        End Try
    End Function

    ''' <summary>
    ''' 读取专用工装信息
    ''' </summary>
    ''' <returns></returns>
    Private Function LoadSpecialToolInfo(ByVal QueryCondition As Dictionary(Of String, String)) As uMESProcessResult Implements uMESDocViewIBusiness.LoadSpecialToolInfo
        Dim oResult As New uMESProcessResult

        Try
            Dim strSQL, strSpecialToolID As String
            strSpecialToolID = ""
            If QueryCondition.Keys.Contains("SpecialToolID") = True Then
                strSpecialToolID = QueryCondition("SpecialToolID").ToString()
            End If
            strSQL = "SELECT XASpecialTool.XASpecialToolname, XASpecialTool.Description, XASpecialTool.Tooltype," & vbCrLf & _
                       "       XASpecialTool.Specifytype, XASpecialTool.Productno, XASpecialTool.Partno, XASpecialTool.Department," & vbCrLf & _
                       "       XASpecialTool.Applyname, XASpecialTool.Applyno" & vbCrLf & _
                       "FROM XASpecialTool" & vbCrLf & _
                       "WHERE XASpecialTool.XASpecialToolID ='" & strSpecialToolID & "'" & vbCrLf

            Dim dtMain As DataTable = dal.GetQualityRecordInfoForProductNo(strSQL)
            oResult.DBTable = dtMain.Copy
            oResult.Result = True
            Return oResult
        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult
        End Try
    End Function

    ''' <summary>
    ''' 读取工序上的通用工具信息
    ''' </summary>
    ''' <returns></returns>
    Private Function LoadSpecCommTool(ByVal QueryCondition As Dictionary(Of String, String)) As uMESProcessResult Implements uMESDocViewIBusiness.LoadSpecCommTool
        Dim oResult As New uMESProcessResult

        Try
            Dim strSQL, strSpecID As String
            strSpecID = ""
            If QueryCondition.Keys.Contains("SpecID") = True Then
                strSpecID = QueryCondition("SpecID").ToString()
            End If
            strSQL = "SELECT XACommTool.XACommToolID, XACommTool.XACommToolname, XACommTool.Description " & vbCrLf & _
                       "FROM XACommTool" & vbCrLf & _
                       "WHERE XACommTool.SpecID ='" & strSpecID & "'" & vbCrLf & _
                       "ORDER BY XACommTool.XACommToolname" & vbCrLf

            Dim dtMain As DataTable = dal.GetQualityRecordInfoForProductNo(strSQL)
            oResult.DBTable = dtMain.Copy
            oResult.Result = True
            Return oResult
        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult
        End Try
    End Function

    ''' <summary>
    ''' 读取通用工具信息
    ''' </summary>
    ''' <returns></returns>
    Private Function LoadCommToolInfo(ByVal QueryCondition As Dictionary(Of String, String)) As uMESProcessResult Implements uMESDocViewIBusiness.LoadCommToolInfo
        Dim oResult As New uMESProcessResult

        Try
            Dim strSQL, strCommToolID As String
            strCommToolID = ""
            If QueryCondition.Keys.Contains("CommToolID") = True Then
                strCommToolID = QueryCondition("CommToolID").ToString()
            End If
            strSQL = "SELECT XACommTool.XACommToolname, XACommTool.Description, XACommTool.Commtooltype," & vbCrLf & _
                 "       XACommTool.Commtoolspec, XACommTool.Commtoolrange, XACommTool.Commtoolprecision" & vbCrLf & _
                 "FROM XACommTool" & vbCrLf & _
                 "WHERE XACommTool.XACommToolID ='" & strCommToolID & "'" & vbCrLf

            Dim dtMain As DataTable = dal.GetQualityRecordInfoForProductNo(strSQL)
            oResult.DBTable = dtMain.Copy
            oResult.Result = True
            Return oResult
        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult
        End Try
    End Function

    ''' <summary>
    ''' 读取CDO基本属性
    ''' </summary>
    ''' <returns></returns>
    Public Function LoadCDODictionary(ByVal QueryCondition As Dictionary(Of String, String)) As uMESProcessResult Implements uMESDocViewIBusiness.LoadCDODictionary
        Dim oResult As New uMESProcessResult
        Try
            Dim strType As String
            strType = ""
            If QueryCondition.Keys.Contains("Type") = True Then
                strType = QueryCondition("Type").ToString()
            End If

            Dim dtDic = New DataTable()
            dtDic.Columns.Add("FieldName")
            dtDic.Columns.Add("FieldValue")
            Dim dr As DataRow

            Dim doc = New XmlDocument()
            Dim strFilePath As String
            strFilePath = ConfigurationSettings.AppSettings("DictionaryConfig") 'oCon.LoadDictionaryConfig
            doc.Load(strFilePath)

            Dim rootNode As XmlNode
            rootNode = doc.DocumentElement
            For Each ParentNode As XmlNode In rootNode.ChildNodes
                Dim strName As String
                strName = ParentNode.Name.Trim()

                If UCase(strName) = UCase(strType) Then
                    If (ParentNode.ChildNodes.Count > 0) Then
                        Dim oNode As XmlNode
                        For Each oNode In ParentNode.ChildNodes

                            dr = dtDic.NewRow()
                            dr(0) = oNode.Attributes(0).Value
                            dr(1) = oNode.Attributes(1).Value
                            dtDic.Rows.Add(dr)
                        Next
                    End If
                End If

            Next

            oResult.DBTable = dtDic.Copy
            oResult.Result = True
            Return oResult
        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult
        End Try
    End Function

    ''' <summary>
    ''' 通过工艺流程读取零件图号
    ''' </summary>
    ''' <returns></returns>
    Private Function LoadProductInfoByWorkflow(ByVal QueryCondition As Dictionary(Of String, String)) As uMESProcessResult Implements uMESDocViewIBusiness.LoadProductInfoByWorkflow
        Dim oResult As New uMESProcessResult

        Try
            Dim strSQL, strWorkflowName, strWorkflowRev As String
            strWorkflowName = ""
            If QueryCondition.Keys.Contains("WorkflowName") = True Then
                strWorkflowName = QueryCondition("WorkflowName").ToString()
            End If
            strWorkflowRev = ""
            If QueryCondition.Keys.Contains("WorkflowRev") = True Then
                strWorkflowRev = QueryCondition("WorkflowRev").ToString()
            End If
            strSQL = "SELECT Productbase.Productname, Product.Productrevision,Product.Description" & vbCrLf & _
                    "FROM Product" & vbCrLf & _
                    "INNER JOIN Productbase ON Product.Productbaseid = Productbase.Productbaseid" & vbCrLf & _
                    "INNER JOIN Workflow ON Workflow.Workflowid = Product.Workflowid" & vbCrLf & _
                    "INNER JOIN Workflowbase ON Workflow.Workflowbaseid = Workflowbase.Workflowbaseid" & vbCrLf & _
                    "WHERE Product.Workflowbaseid = '0000000000000000'" & vbCrLf & _
                    "AND Workflowbase.Workflowname ='" & strWorkflowName & "'" & vbCrLf & _
                    "AND Workflow.Workflowrevision = '" & strWorkflowRev & "'" & vbCrLf & _
                    "UNION ALL" & vbCrLf & _
                    "SELECT Productbase.Productname, Product.Productrevision, Product.Description" & vbCrLf & _
                    "FROM Product" & vbCrLf & _
                    "INNER JOIN Productbase ON Product.Productbaseid = Productbase.Productbaseid" & vbCrLf & _
                    "INNER JOIN Workflowbase ON Product.Workflowbaseid = Workflowbase.Workflowbaseid" & vbCrLf & _
                    "INNER JOIN Workflow ON Workflow.Workflowid = Workflowbase.Revofrcdid" & vbCrLf & _
                    "WHERE Product.Workflowid = '0000000000000000'" & vbCrLf & _
                    "AND Workflowbase.Workflowname ='" & strWorkflowName & "'" & vbCrLf & _
                    "AND Workflow.Workflowrevision = '" & strWorkflowRev & "'" & vbCrLf

            Dim dtMain As DataTable = dal.GetQualityRecordInfoForProductNo(strSQL)
            oResult.DBTable = dtMain.Copy
            oResult.Result = True
            Return oResult
        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult
        End Try
    End Function

    Private Function LoadStepInfo(ByVal QueryCondition As Dictionary(Of String, String)) As uMESProcessResult Implements uMESDocViewIBusiness.LoadStepInfo
        Dim oResult As New uMESProcessResult

        Try

            Dim strStepID As String = ""
            If QueryCondition.Keys.Contains("StepID") = True Then
                strStepID = QueryCondition("StepID").ToString()
            End If
            Dim strSQL As String = "select sb.stepinfoname,t.revision,t.stepno from stepinfo t left join stepinfobase sb on sb.stepinfobaseid=t.stepinfobaseid " +
                                  "where t.stepinfoid = '" & strStepID & "'"

            Dim tmpSet As DataSet = dal.ExecuteQuery(strSQL)
            Dim dt As DataTable = tmpSet.Tables(0).Copy()

            oResult.DBTable = dt.Copy
            oResult.Result = True
            Return oResult
        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult
        End Try
    End Function


End Class

